import { getGoodsInfo, addCart } from 'api/detail'
const state = {
  // 商品详情信息
  goodsInfo: {}
}
const mutations = {
  setGoodsInfo (state, payload) {
    state.goodsInfo = payload
  }
}
const getters = {
  skuInfo (state) {
    const skuInfo = state?.goodsInfo?.skuInfo || {}
    return skuInfo
  },
  categoryView (state) {
    const categoryView = state?.goodsInfo?.categoryView || {}
    return categoryView
  },
  skuImageList (state, getters) {
    const skuImageList = getters?.skuInfo?.skuImageList || []
    return skuImageList
  },
  // 商品详情属性
  shopAttr (state) {
    const shopAttr = state?.goodsInfo?.spuSaleAttrList || []
    return shopAttr
  }
}
const actions = {
  // 得到商品信息
  async onGetGoodsInfo ({ commit }, shopId) {
    const { data: res } = await getGoodsInfo(shopId)
    commit('setGoodsInfo', res.data)
  },
  async onAddCart (context, { shopId, skuNum }) {
    const { data: res } = await addCart(shopId, skuNum)
    if (res.code === 200) {
      return Promise.resolve('ok')
    } else {
      return Promise.reject(new Error('fail'))
    }
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
